package com.example.rabbitmq.comsumer.topic.config;

import com.example.common.config.RabbitMQConfig;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 
 *
 * @author chenzhian
 * @version 0.1.0
 * @since 0.1.0
 */
@Configuration
public class TopicConfig {

  @Bean
  public Queue queueMessage() {
    return new Queue(RabbitMQConfig.RABBITMQ_TOPIC_QUEUE_A, true, false, false);
  }

  @Bean
  public Queue queueMessages() {
    return new Queue(RabbitMQConfig.RABBITMQ_TOPIC_QUEUE_B, true, false, false);
  }

  @Bean
  TopicExchange topicExchange() {
    return new TopicExchange(RabbitMQConfig.RABBITMQ_TOPIC_EXCHANGE);
  }


  /**
   * 把队列绑定到交换机
   */
  @Bean
  public Binding bindExchangeQueueMessage() {
    //链式写法，绑定交换机和队列，并设置匹配键
    return BindingBuilder
        //绑定队列
        .bind(queueMessage())
        //到交换机
        .to(topicExchange())
        //并设置匹配键
        .with("topic.a");
  }

  @Bean
  public Binding bindExchangeQueues() {
    //链式写法，绑定交换机和队列，并设置匹配键
    return BindingBuilder
        //绑定队列
        .bind(queueMessages())
        //到交换机
        .to(topicExchange())
        //并设置匹配键
        .with("topic.#");
  }
}
